package.cpath = package.cpath .. ';C:/Users/win/AppData/Roaming/JetBrains/IdeaIC2020.3/plugins/intellij-emmylua/classes/debugger/emmy/windows/x86/?.dll'
local dbg = require('emmy_core')
dbg.tcpConnect('localhost', 9966)

package.path = package.path .. ";C:/Users/win/Documents/_lua/util/?.lua"

local Queue = require("Queue")


function Test1()
    local q = Queue.new()
    assert(0 == q:GetCount())

    q:Enqueue(1)
    assert(1 == q:GetCount())
    q:Enqueue(2)
    assert(2 == q:GetCount())
    q:Enqueue(3)
    assert(3 == q:GetCount())
    q:Enqueue(4)
    assert(4 == q:GetCount())
    q:Enqueue(5)
    assert(5 == q:GetCount())
    assert("1,2,3,4,5" == tostring(q))

    assert(1 == q:PeekFirst())
    assert(5 == q:GetCount())
    assert(5 == q:PeekLast())
    assert(5 == q:GetCount())

    assert(1 == q:Dequeue())
    assert(4 == q:GetCount())
    assert(2 == q:PeekFirst())
    assert(5 == q:PeekLast())

    assert(2 == q:Dequeue())
    assert(3 == q:GetCount())
    assert(3 == q:PeekFirst())
    assert(5 == q:PeekLast())

    assert("3,4,5" == tostring(q))

    q:Enqueue(6)
    assert(4 == q:GetCount())
    assert(3 == q:PeekFirst())
    assert(6 == q:PeekLast())

    q:Enqueue(7)
    assert(5 == q:GetCount())
    assert(3 == q:PeekFirst())
    assert(7 == q:PeekLast())

    q:Enqueue(8)
    assert(6 == q:GetCount())
    assert(3 == q:PeekFirst())
    assert(8 == q:PeekLast())

    assert("3,4,5,6,7,8" == tostring(q))

    q:Enqueue(9)
    assert(7 == q:GetCount())
    assert(3 == q:PeekFirst())
    assert(9 == q:PeekLast())

    assert(3 == q:Dequeue())
    assert(6 == q:GetCount())

    assert(4 == q:Dequeue())
    assert(5 == q:GetCount())

    assert(5 == q:Dequeue())
    assert(4 == q:GetCount())

    assert(6 == q:Dequeue())
    assert(3 == q:GetCount())

    assert(7 == q:Dequeue())
    assert(2 == q:GetCount())

    assert(8 == q:Dequeue())
    assert(1 == q:GetCount())

    assert(9 == q:Dequeue())
    assert(0 == q:GetCount())

    assert("" == tostring(q))

    assert(nil == q:Dequeue())
    assert(0 == q:GetCount())

    assert("" == tostring(q))
end
Test1()


function Test2()
    local q = Queue.new()

    q:Enqueue(1)
    q:Enqueue(2)
    q:Enqueue(3)
    q:Enqueue(4)
    q:Enqueue(5)
    q:Enqueue(6)

    assert("1,2,3,4,5,6" == tostring(q))

    q:Dequeue()
    q:Dequeue()
    assert(4 == q:GetCount())

    assert("3,4,5,6" == tostring(q))

    q:Enqueue(7)
    assert(5 == q:GetCount())
    assert(3 == q:PeekFirst())
    assert(7 == q:PeekLast())

    q:Enqueue(8)
    assert(6 == q:GetCount())
    assert(3 == q:PeekFirst())
    assert(8 == q:PeekLast())

    q:Enqueue(9)
    assert(7 == q:GetCount())
    assert(3 == q:PeekFirst())
    assert(9 == q:PeekLast())

    assert("3,4,5,6,7,8,9" == tostring(q))

    assert(3 == q:Dequeue())
    assert(6 == q:GetCount())

    assert(4 == q:Dequeue())
    assert(5 == q:GetCount())

    assert(5 == q:Dequeue())
    assert(4 == q:GetCount())

    assert(6 == q:Dequeue())
    assert(3 == q:GetCount())

    assert(7 == q:Dequeue())
    assert(2 == q:GetCount())

    assert(8 == q:Dequeue())
    assert(1 == q:GetCount())

    assert(9 == q:Dequeue())
    assert(0 == q:GetCount())

    assert(nil == q:Dequeue())
    assert(0 == q:GetCount())

    assert("" == tostring(q))
end
Test2()

function Test3()
    local q = Queue.new()

    q:Enqueue(1)
    q:Enqueue(2)
    q:Enqueue(3)
    q:Enqueue(4)
    q:Enqueue(5)
    q:Enqueue(6)

    q:Dequeue()
    q:Dequeue()
    assert(4 == q:GetCount())

    q:Clear()
    assert(0 == q:GetCount())
    assert(nil == q:Dequeue())
end
Test3()

function Test4()
    local q = Queue.new()

    q:Enqueue(1)
    q:Enqueue(2)
    q:Enqueue(3)
    q:Enqueue(4)
    q:Enqueue(5)
    q:Enqueue(6)

    for v in q:GetIterator() do
        print("Test4", v)
    end

    assert(0 == q:GetCount())
    assert(nil == q:Dequeue())
end
Test4()

function Test5()
    local q = Queue.new()

    q:Enqueue(1)
    q:Enqueue(2)
    q:Enqueue(3)
    q:Enqueue(4)
    q:Dequeue()
    q:Dequeue()

    q:Enqueue(5)

    assert(3 == q:Dequeue())
    assert(2 == q:GetCount())

    assert(4 == q:Dequeue())
    assert(1 == q:GetCount())
    assert(5 == q:Dequeue())
    assert(0 == q:GetCount())

    assert(nil == q:Dequeue())
end
Test5()